package com.zzoranor.presentationserver;

import java.net.Socket;
import java.util.Vector;

import protocols.ProtocolsProto.ClientToServerMessage;

 

public class ServerDispatcher extends Thread

{

    private Vector<String> messageQueue = new Vector<String>();

    private Vector<ClientInfo> clients = new Vector<ClientInfo>();

 

    /**

     * Adds given client to the server's client list.

     */

    public synchronized void addClient(ClientInfo aClientInfo)

    {

    	clients.add(aClientInfo);

    }

 

    /**

     * Deletes given client from the server's client list

     * if the client is in the list.

     */

    public synchronized void deleteClient(ClientInfo aClientInfo)

    {
        clients.removeElement(aClientInfo);
    }

 

    /**

     * Adds given message to the dispatcher's message queue and notifies this

     * thread to wake up the message queue reader (getNextMessageFromQueue method).

     * dispatchMessage method is called by other threads (ClientListener) when

     * a message is arrived.

     */

    public synchronized void dispatchMessage(ClientInfo aClientInfo, String aMessage)

    {

        Socket socket = aClientInfo.getSocket();

        String senderIP = socket.getInetAddress().getHostAddress();

        String senderPort = "" + socket.getPort();

        aMessage = senderIP + ":" + senderPort + " : " + aMessage;

        messageQueue.add(aMessage);

        notify();

    }

 

    /**

     * @return and deletes the next message from the message queue. If there is no

     * messages in the queue, falls in sleep until notified by dispatchMessage method.

     */

    private synchronized String getNextMessageFromQueue()

    throws InterruptedException

    {

        while (messageQueue.size()==0)

           wait();

        String message = (String) messageQueue.get(0);

        messageQueue.removeElementAt(0);

        return message;

    }

 

    /**

     * Sends given message to all clients in the client list. Actually the

     * message is added to the client sender thread's message queue and this

     * client sender thread is notified.

     */

    private synchronized void sendMessageToAllClients(String aMessage)

    {

        for (int i=0; i<clients.size(); i++) {

           ClientInfo clientInfo = (ClientInfo) clients.get(i);

           clientInfo.getSender().sendMessage(aMessage);
           

        }

    }

 

    /**

     * Infinitely reads messages from the queue and dispatch them

     * to all clients connected to the server.

     */

    public void run()

    {

        try {

           while (true) {

               String message = getNextMessageFromQueue();

               sendMessageToAllClients(message);

           }

        } catch (InterruptedException ie) {

           // Thread interrupted. Stop its execution

        }

    }

 

}